package cn.zhoutao.basic.controller;

import cn.zhoutao.basic.dto.RegisterDto;
import cn.zhoutao.basic.exception.BusinessException;
import cn.zhoutao.basic.service.IVerifyCodeService;
import cn.zhoutao.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:MrZhou
 * @Date:2022/6/15 21:45
 * 1.图片验证码
 * 2.短信验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 发送图片验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String key){
        try {
            String base64 = verifyCodeService.imageCode(key);//base64是base64编码的字符串
            return AjaxResult.me().setResultObj(base64);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存店铺失败！"+e.getMessage());
        }
    }

    /**
     * 发送短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

}
